package com.braska.hewankurban;

import com.braska.hewankurban.LoginActivity.LoginTask;
import com.braska.hewankurban.helper.ServiceHelper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginDokterActivity extends Activity {
	ServiceHelper srv = new ServiceHelper();

	EditText name, nik;
	String n;
	int k;
	Intent i;
	String jenishewan, nama, berat, umur, cacat, id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_logindokter);
		Button btn_masuk = (Button) findViewById(R.id.btnLogin);
		nik = (EditText) findViewById(R.id.inputPassdokter);
		name = (EditText) findViewById(R.id.inputUsrdokter);

		Bundle extras = getIntent().getExtras();
		id = extras.getString("id");
		jenishewan = extras.getString("hewan");
		nama = extras.getString("nama");
		berat = extras.getString("berat");
		umur = extras.getString("umur");
		cacat = extras.getString("cacat");

		btn_masuk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				n = name.getText().toString();
				k = Integer.parseInt(nik.getText().toString());
				Log.i("nik", k+"");
				new LoginTask().execute();
			}
		});
	}

	public class LoginTask extends AsyncTask<String, Void, Boolean> {
		ProgressDialog dialog = new ProgressDialog(LoginDokterActivity.this);

		protected void onPreExecute() {
			dialog.setMessage(" Login ");
			dialog.show();
		}

		@Override
		protected Boolean doInBackground(String... params) {
			Log.d("nama + nik ", n+" "+k);
			// TODO Auto-generated method stub
			if (srv.logindokter(n, k)) {
				return true;
			} else {
				return false;
			}
		}

		protected void onPostExecute(Boolean result) {
			dialog.dismiss();
			if (result == true) {
				Toast.makeText(LoginDokterActivity.this, "Login Success",
						Toast.LENGTH_LONG).show();
				i = new Intent(getApplicationContext(),
						PeriksaDokterActivity.class);
				i.putExtra("nama", nama);
				i.putExtra("berat", berat);
				i.putExtra("umur", umur);
				i.putExtra("cacat", cacat);
				i.putExtra("hewan", jenishewan);
				i.putExtra("id", id);
				
				Log.i("1", id);
				Log.i("2", nama);
				Log.i("3", berat);
				Log.i("4", jenishewan);
				Log.i("5", cacat);
				Log.i("6", umur);
				startActivity(i);
				finish();

			} else {

				Toast.makeText(LoginDokterActivity.this, "Login Failed",
						Toast.LENGTH_LONG).show();
			}

		}

	}

}
